<?php
    class Admincp_RoomController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        private $_locale;
        
        public function init()
        {
            $this->loadTemplates('admin');
            $this->_helper->layout->setLayout('popup');
            $this->view->headTitle('Quản lý phòng');
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Admincp_Model_Room();
            $this->_locale = 'vi';
        }
        
        public function preDispatch()
		{
		    $auth = Zend_Auth::getInstance();
		    if(!$auth->hasIdentity()) {
		        if($this->_request->getActionName() != 'login')
		            $this->_redirect('admincp/index/login');
		    }
		}
        
        public function indexAction()
        {
            $type = (int) $this->_arrParam['type'];
            $hotel_id = (int) $this->_arrParam['hotel_id'];
            
            if($_POST['add']) {
                $this->_redirect('admincp/room/create?type='.$type.'&hotel_id='.$hotel_id);
            }
            if($_POST['delete']) {
                $listId = $this->_arrParam['check'];
                $this->_model->deleteMultiItem($listId);
                $this->_redirect('admincp/room/index?type='.$type.'&hotel_id='.$hotel_id);
            }
            
            $data = array(
                'type' => $type,
                'hotel_id' => $hotel_id,
            );

            $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->listItem($data));
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(40);
            $paginator->setPageRange(5);
            $currentPage = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->room = $data;
            $this->view->data = $paginator;
            $this->view->total = $paginator->getTotalItemCount();
        }
        
        public function createAction()
        {
            $hotel_id = $this->_arrParam['hotel_id'];
            $type = $this->_arrParam['type'];
            $data = array(
                'type' => $type,
                'hotel_id' => $hotel_id,
            );
            $language = new Admincp_Model_Language();
            $this->view->language = $language->listLanguageFull();
            $this->view->room = $data;
            
            if($this->_request->isPost()) {
                $name['vi']     = trim(strip_tags($this->_arrParam['name-vi']));
                $name['zh']     = trim(strip_tags($this->_arrParam['name-zh']));
                $name['en']     = trim(strip_tags($this->_arrParam['name-en']));
                $name['trv']     = trim(strip_tags($this->_arrParam['name-trv']));
                $description['vi'] = trim($this->_arrParam['description-vi']);
                $description['zh'] = trim($this->_arrParam['description-zh']);
                $description['en'] = trim($this->_arrParam['description-en']);
                $description['trv'] = trim($this->_arrParam['description-trv']);
                $max = (int) $this->_arrParam['max'];
                $price = (int) ($this->_arrParam['price'].'000');
                $hotel_id = $this->_arrParam['hotel_id'];
                $type = $this->_arrParam['type'];
                
                // Insert to database
                $strFlex = new Zendvn_Filter_RemoveCircumflex();
                $data_room = array(
                    'hotel_id' => $hotel_id,
                    'type' => $type,
                    'max' => $max,
                    'price' => $price,
                    'status' => 1,
                );
                $room_id = $this->_model->saveItem($data_room);
                foreach($name as $key=>$value) {
                    foreach($this->view->language as $lang) {
                        if($key == $lang['locale']) {
                            $data = array(
                                'room_id' => $room_id,
                                'language_id' => $lang['id'],
                                'name' => $value,
                                'description' => $description[$key],
                            );
                            $this->_model->saveItem($data, 'language');
                            break;
                        }
                    }
                }
                $this->_redirect('admincp/room/index?type='.$type.'&hotel_id='.$hotel_id);
            }
        }
        
        public function editAction()
        {
            $id = (int) $this->_arrParam['id'];
            $hotel_id = $this->_arrParam['hotel_id'];
            $type = $this->_arrParam['type'];
            $data = array(
                'type' => $type,
                'hotel_id' => $hotel_id,
            );
            $language = new Admincp_Model_Language();
            $this->view->language = $language->listLanguageFull();
            $this->view->data = $this->_model->getItem($id, 'language');
            $this->view->data2 = $this->_model->getItem($id);
            $this->view->room = $this->view->data2;
            
            if($this->_request->isPost()) {
                $name['vi']     = trim(strip_tags($this->_arrParam['name-vi']));
                $name['zh']     = trim(strip_tags($this->_arrParam['name-zh']));
                $name['en']     = trim(strip_tags($this->_arrParam['name-en']));
                $name['trv']     = trim(strip_tags($this->_arrParam['name-trv']));
                $description['vi'] = trim($this->_arrParam['description-vi']);
                $description['zh'] = trim($this->_arrParam['description-zh']);
                $description['en'] = trim($this->_arrParam['description-en']);
                $description['trv'] = trim($this->_arrParam['description-trv']);
                $max = (int) $this->_arrParam['max'];
                $price = (int) ($this->_arrParam['price'].'000');
                $hotel_id = $this->_arrParam['hotel_id'];
                $type = $this->_arrParam['type'];
                
                // Insert to database
                $strFlex = new Zendvn_Filter_RemoveCircumflex();
                $data_room = array(
                    'id' => $id,
                    'max' => $max,
                    'price' => $price,
                );
                $this->_model->updateItem($data_room);
                foreach($name as $key=>$value) {
                    foreach($this->view->language as $lang) {
                        if($key == $lang['locale']) {
                            $data = array(
                                'room_id' => $id,
                                'language_id' => $lang['id'],
                                'name' => $value,
                                'description' => $description[$key],
                            );
                            $this->_model->updateItem($data, 'language');
                            break;
                        }
                    }
                }
                $this->_redirect('admincp/room/index?type='.$type.'&hotel_id='.$hotel_id);
            }
        }
        
        public function deleteAction()
        {
            $id = (int) $this->_arrParam['id'];
            $type = (int) $this->_arrParam['type'];
            $hotel_id = (int) $this->_arrParam['hotel_id'];
            $this->_model->deleteItem($id);
            $this->_redirect('admincp/room/index?type='.$type.'&hotel_id='.$hotel_id);
        }
        
        public function publishAction()
        {
            $id = (int) $this->_arrParam['id'];
            $status = (int) $this->_arrParam['status'];
            $status = ($status == 1) ? 2 : 1;
            $data = array(
                'id'=>$id,
                'status'=>$status,
            );
            $this->_model->updateItem($data);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
    }
